package com.ruoyi.web.controller.pull;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ERP.MaterialDataErp;
import com.ruoyi.system.domain.Pull.MaterialFeed;
import com.ruoyi.system.domain.Pull.MaterialPlug;
import com.ruoyi.system.domain.base.BaseShop;
import com.ruoyi.system.mapper.BaseShopMapper;
import com.ruoyi.system.mapper.MaterialDataErpMapper;
import com.ruoyi.system.mapper.pull.MaterialPlugMapper;
import com.ruoyi.system.service.WMSService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IMaterialPlugService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 补料记录Controller
 *
 * @author ruoyi
 * @date 2023-03-06
 */
@Api(value = "MaterialPlugController", tags = "补料记录接口api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pull/plug" )
public class MaterialPlugController extends BaseController {

    private final IMaterialPlugService iMaterialPlugService;

    /**
     * 查询补料记录列表
     */
    @ApiOperation(value = "查询补料记录列表",response = MaterialPlug.class)
    @GetMapping("/list")
    public TableDataInfo list(MaterialPlug materialPlug)
    {
        startPage();
        LambdaQueryWrapper<MaterialPlug> lqw = new LambdaQueryWrapper<MaterialPlug>();
        if (StringUtils.isNotBlank(materialPlug.getPlugNumbers())){
            lqw.eq(MaterialPlug::getPlugNumbers ,materialPlug.getPlugNumbers());
        }
        if (StringUtils.isNotBlank(materialPlug.getMaterialName())){
            lqw.like(MaterialPlug::getMaterialName ,materialPlug.getMaterialName());
        }
        if (StringUtils.isNotBlank(materialPlug.getMaterialCode())){
            lqw.like(MaterialPlug::getMaterialCode ,materialPlug.getMaterialCode());
        }
        if (StringUtils.isNotBlank(materialPlug.getLineName())){
            lqw.like(MaterialPlug::getLineName ,materialPlug.getLineName());
        }
        if (StringUtils.isNotBlank(materialPlug.getStationName())){
            lqw.like(MaterialPlug::getStationName ,materialPlug.getStationName());
        }
        if (StringUtils.isNotBlank(materialPlug.getDeptName())){
            lqw.like(MaterialPlug::getDeptName ,materialPlug.getDeptName());
        }
        if (StringUtils.isNotBlank(materialPlug.getOrderCode())){
            lqw.eq(MaterialPlug::getOrderCode ,materialPlug.getOrderCode());
        }
        if (StringUtils.isNotBlank(materialPlug.getTaskTon())){
            lqw.eq(MaterialPlug::getTaskTon ,materialPlug.getTaskTon());
        }
        if (StringUtils.isNotBlank(materialPlug.getProductCode())){
            lqw.eq(MaterialPlug::getProductCode ,materialPlug.getProductCode());
        }
        lqw.orderByDesc(MaterialPlug::getPlugId);
        List<MaterialPlug> list = iMaterialPlugService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出补料记录列表
     */
    @Log(title = "补料记录" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(MaterialPlug materialPlug) {
        LambdaQueryWrapper<MaterialPlug> lqw = new LambdaQueryWrapper<MaterialPlug>(materialPlug);
        List<MaterialPlug> list = iMaterialPlugService.list(lqw);
        ExcelUtil<MaterialPlug> util = new ExcelUtil<MaterialPlug>(MaterialPlug. class);
        return util.exportExcel(list, "plug" );
    }

    /**
     * 获取补料记录详细信息
     */
    @ApiOperation(value = "获取补料记录详细信息")
    @GetMapping(value = "/{plugId}" )
    public AjaxResult getInfo(@PathVariable("plugId" ) Long plugId) {
        return AjaxResult.success(iMaterialPlugService.getById(plugId));
    }

    @Autowired
    private MaterialDataErpMapper materialDataErpMapper;
    @Autowired
    private BaseShopMapper baseShopMapper;
    /**
     * 新增补料记录
     */
    @ApiOperation(value = "新增补料记录")
    @Log(title = "补料记录" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialPlug materialPlug) {
        materialPlug.setCreateBy(SecurityUtils.getUsername());
        materialPlug.setCreateTime(new Date());
        materialPlug.setDeptName(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
        materialPlug.setPlugNumbers(new Date().getTime()+"");

        //根据物料编码获取物料名称
        MaterialDataErp ma = materialDataErpMapper.selectOne(new QueryWrapper<MaterialDataErp>().eq("data_id", materialPlug.getDataId()));
        if(ObjectUtil.isNotNull(ma)){
            materialPlug.setMaterialCode(ma.getInvCode());
            materialPlug.setMaterialName(ma.getInvName());
        }

        //根据工位获取产线编码与名称
        BaseShop shop = baseShopMapper.selectOne(new QueryWrapper<BaseShop>().eq("shop_code", materialPlug.getStationCode()));
        if(ObjectUtil.isNotNull(shop)){
            BaseShop shop1 = baseShopMapper.selectOne(new QueryWrapper<BaseShop>().eq("shop_id", shop.getParentId()));
            if(ObjectUtil.isNotNull(shop1)){
                materialPlug.setLineCode(shop1.getShopCode());
                materialPlug.setLineName(shop1.getShopName());
            }
        }

        return toAjax(iMaterialPlugService.save(materialPlug) ? 1 : 0);
    }

    /**
     * 修改补料记录
     */
    @ApiOperation(value = "修改补料记录")
    @Log(title = "补料记录" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialPlug materialPlug) {
        materialPlug.setUpdateBy(SecurityUtils.getUsername());
        materialPlug.setUpdateTime(new Date());

        //根据物料编码获取物料名称
        MaterialDataErp ma = materialDataErpMapper.selectOne(new QueryWrapper<MaterialDataErp>().eq("data_id", materialPlug.getDataId()));
        if(ObjectUtil.isNotNull(ma)){
            materialPlug.setMaterialCode(ma.getInvCode());
            materialPlug.setMaterialName(ma.getInvName());
        }

        //根据工位获取产线编码与名称
        BaseShop shop = baseShopMapper.selectOne(new QueryWrapper<BaseShop>().eq("shop_code", materialPlug.getStationCode()));
        if(ObjectUtil.isNotNull(shop)){
            BaseShop shop1 = baseShopMapper.selectOne(new QueryWrapper<BaseShop>().eq("shop_id", shop.getParentId()));
            if(ObjectUtil.isNotNull(shop1)){
                materialPlug.setLineCode(shop1.getShopCode());
                materialPlug.setLineName(shop1.getShopName());
            }
        }

        return toAjax(iMaterialPlugService.updateById(materialPlug) ? 1 : 0);
    }

    /**
     * 删除补料记录
     */
    @ApiOperation(value = "删除补料记录")
    @Log(title = "补料记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{plugIds}" )
    public AjaxResult remove(@PathVariable Long[] plugIds) {
        return toAjax(iMaterialPlugService.removeByIds(Arrays.asList(plugIds)) ? 1 : 0);
    }

    @Autowired
    private WMSService wmsService;
    @PostMapping("/materialIssued")
    @ApiOperation(value = "物料下发至WMS")
    public AjaxResult materialIssued(@RequestBody List<Long> plugIds) {
        AjaxResult ajaxResult = wmsService.materialIssued(plugIds);
        return ajaxResult;
    }




}
